package com.hua.giftool.main.activity

import android.os.Build
import android.os.Bundle
import android.webkit.URLUtil
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.hua.giftool.R
import com.hua.giftool.base.BaseActivity
import com.hua.giftool.main.viewmodel.MainViewModel
import com.hua.giftool.ui.screen.VideoToGifScreen
import com.hua.giftool.ui.theme.GifToolTheme
import com.hua.giftool.utils.DownloadController
import com.hua.giftool.utils.checkOrRequestPermission

class MainActivity : BaseActivity() {
    private val viewModel: MainViewModel by viewModels()
    private val downloadController by lazy { DownloadController(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            GifToolTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    VideoToGifScreen()
                }

                val updateInfo by viewModel.updateInfo.collectAsState()
                var showUpdateDialog by remember { mutableStateOf(false) }

                LaunchedEffect(updateInfo) {
                    if (updateInfo != null) {
                        showUpdateDialog = true
                    }
                }

                if (showUpdateDialog) {
                    val info = updateInfo!!
                    AlertDialog(
                        onDismissRequest = { showUpdateDialog = false },
                        title = { Text(stringResource(R.string.update_dialog_title)) },
                        text = { Text(stringResource(R.string.update_dialog_message, info.versionName)) },
                        confirmButton = {
                            TextButton(
                                onClick = {
                                    val fileName = URLUtil.guessFileName(info.downloadUrl, null, null)
                                    downloadController.enqueueDownload(info.downloadUrl, fileName)
                                    showUpdateDialog = false
                                }
                            ) {
                                Text(stringResource(R.string.update_button))
                            }
                        },
                        dismissButton = {
                            TextButton(onClick = { showUpdateDialog = false }) {
                                Text(stringResource(R.string.cancel_button))
                            }
                        }
                    )
                }
            }
        }
        checkPermission()
        viewModel.checkForUpdate()
    }

    private fun checkPermission() {
        when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> checkOrRequestPermission(
                mutableListOf(
                    android.Manifest.permission.READ_MEDIA_IMAGES,
//                    android.Manifest.permission.READ_MEDIA_AUDIO,
                    android.Manifest.permission.READ_MEDIA_VIDEO
                ),
                {}
            )

            Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> checkOrRequestPermission(
                mutableListOf(
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    android.Manifest.permission.MANAGE_EXTERNAL_STORAGE
                ),
                {}
            )

            else -> checkOrRequestPermission(
                mutableListOf(
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE
                ),
                {}
            )
        }
    }
}
